import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, Comparator.comparingInt((v) -> v[0]));
        List<int[]> ans_list = new ArrayList<>();
        int curStart = intervals[0][0];
        int curEnd = intervals[0][1];
        for(int[] each : intervals) {
            if (each[0] <= curEnd) {
                curEnd = Math.max(curEnd, each[1]);
            } else {
                ans_list.add(new int[]{curStart, curEnd});
                curStart = each[0];
                curEnd = each[1];
            }
        }
        ans_list.add(new int[]{curStart, curEnd});

//        int[][] ans = new int[ans_list.size()][];
//        for (int i = 0; i < ans.length; i++) {
//            ans[i] = ans_list.get(i);
//        }
        int[][] ans = new int[ans_list.size()][];
        ans_list.toArray(ans);
        return ans;
    }
}